<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>基础04</title>
    <style>
        #box{
            width: 100px;
            height: 100px;
            background-color: cyan;
        }
    </style>
</head>
<body>
<div id="box" style="display: block"></div>
<button onClick='handleClick'>点击按钮</button>
<script>
    const Obox = document.querySelector('#box')
    const Obtn = document.querySelector('button')
    Obtn.addEventListener('click',handleClick)
    let obj ={
        isShow:true
    }
    function handleClick() {
        console.log('click')
        console.log(obj.isShow)
        obj.isShow = !obj.isShow
    }


    function observer(obj) {
        let handler = {
            get(target, key, receiver) {
                if (typeof target[key] === "object" && target[key] !== null) {
                    // 这里
                    return new Proxy(target[key], handler);
                }
                return Reflect.get(target, key, receiver);
            },
            set(target, key, value, receiver) {
                console.log(key + "-- 数据发生改变");
                if(target[key]){
                    // Obox.style.display = 'block'
                    Obox.style.background = 'rgba(0,0,0,0.7)'
                    Obox.style.transition = 'background 1s ease'
                }else{
                    // Obox.style.display = 'none'
                    Obox.style.background = 'cyan'
                    Obox.style.transition = ''
                }
                return Reflect.set(target, key, value, receiver);
            },
        };

        return new Proxy(obj, handler);
    }

    obj = observer(obj)


</script>
</body>
</html>
